#include <stdio.h>
#include <stdlib.h>
#include "application_entry.h"
#include "key_input_task.h"
#include "server_input_task.h"
#include "iotgo_service.h"
#include "platformAPI.h"
#include "system_monitor_task.h"
#include "gprs_command.h"
#include "gprs_uart_interface.h"
#include "gprs_task.h"


#define tskController_PRIORITY (tskIDLE_PRIORITY + 8)


static int8 is_device_info_ready(void)
{
    uint8 device_model[12] = {0};
    uint8 device_id[12] = {0};
    uint8 api_key[40] = {0};

    getDeviceInfo(device_id, api_key);
    getDeviceModel(device_model);
    if(device_id[11] == 5 &&
       device_model[11] == 5 &&
       api_key[37] == 5 &&
       api_key[39] == 5)
    {
        return 0;
    }
    return -1;
}

static void test_mode(void)
{
    uint8 count = 0;
    uint8 i = 0;
    int8 ret = -1;
    /*test all the io*/
    #if 0
    gpio_t led_for_test[13];
    PinName led_name[13] = {PA_0,PA_1,PA_2,PA_4,PA_5,PC_0,PC_1,PC_2,PC_3,PC_4,PC_5,PB_2,PB_3};
    
    for(count = 0; count < 13; count++)
    {
        gpio_init(&led_for_test[count],led_name[count]);
        gpio_dir(&led_for_test[count],PIN_OUTPUT);
        gpio_mode(&led_for_test[count],PullUp);
        gpio_write(&led_for_test[count],1);
    }
    /*connect to an wifi AP*/
    while(i < 10)
    {
            ret = connectToWifi("ITEAD_PD_T", "27955416");
            if(ret == 0)
            {
                for(count = 0; count < 13; count++)
                {
                    gpio_write(&led_for_test[count],0);
                }
                break;
            }
            i++;
    }
    #endif
}


static int8_t gpio0_pressed(void)
{
    uint8_t level = 0;
    PIN_FUNC_SELECT(GPIO_PIN_REG(0),FUNC_GPIO0);
    GPIO_DIS_OUTPUT(GPIO_ID_PIN(0));
    
    level = GPIO_INPUT_GET(GPIO_ID_PIN(0));
    if(level == 0)
    {
	systemDelayMs(50);
    }

    level = GPIO_INPUT_GET(GPIO_ID_PIN(0));
    if(level == 0)
    {
	return 0;
    }
    return 1;
}


static void application_controller(void *param)
{
    uint8_t mode = 0;
    int8_t ret = 0;

    
    openGprsUart(115200);
    printf("SDK version:%s\n", system_get_sdk_version());
    printf("application start\n");

    startGprsRuntime();
    startIotgoSystemMonitorTask();
	

    while(gprsReset() != 0)
    {
	systemDelayMs(3000);
    }
    printf("[%s] test0\n",__FUNCTION__);
    ret = is_device_info_ready();
    ret = 0;
    if(ret == 0) /*device info has been ready, do the task*/
    {
        printf("[%s] start tasks\n",__FUNCTION__);
	system_mode = SYS_WORKING_MODE;
	initRelay();
	startServerInputTask();
	startKeyInputTask();
	//myRTCstart();
	//startUserTimerTask();
    }
    else /*device info is not ready, */
    {
        system_mode = SYS_FLASH_DATA_MODE;
	//startIotgoSystemMonitorTask();
	//logUartServerInit();
	test_mode();
    }
    
    vTaskDelete(NULL);

#if 0
    while(1)
    {
	streamParseRuntime();
	//system_print_meminfo();
	//printf("[%s] free heap size [%d]\n",__FUNCTION__,system_get_free_heap_size());
	systemDelayMs(50);
    }
#endif
}


void application_entry(void)
{
/*start the application controller*/
if(xTaskCreate(application_controller, 
		   ((const char*)"application_controller"), 
		   2500, 
		   NULL, 
		   tskController_PRIORITY, 
		   NULL) != pdPASS)
    {
        printf("\n\r%s xTaskCreate(application_controller) failed", __FUNCTION__);
    }	
}
